<template>
    <div>
        <h1 style="text-align:center;">注册</h1>
        <van-form @submit="onSubmit">
            <van-field v-model="username" name="username" label="用户名" placeholder="用户名" :rules="rules.username" />
            <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                :rules="rules.password" />
            <van-field v-model="repass" type="password" name="repass" label="确认密码" placeholder="确认密码"
                :rules="rules.repass" />
            <van-field v-model="email" name="email" label="邮箱" placeholder="邮箱" :rules="rules.email" />
            <van-field v-model="tel" name="tel" label="手机号" placeholder="手机号" :rules="rules.tel" />
            <div style="margin: 16px;">
                <van-button round block color="orangered" type="info" native-type="submit">提交</van-button>
            </div>
        </van-form>
        <div style="text-align:center;">
            <a href="#/my/login">已有账号？去登录！</a>
        </div>
    </div>
</template>

<script setup>
// import Vue from 'vue';
import { Form, Field, Button } from 'vant';
import { register } from '@/request/request'
import { Toast } from 'vant';
import { showSuccessToast, showFailToast } from 'vant';
import 'vant/es/toast/style';

import { useRouter, useRoute } from 'vue-router'
import { createApp, ref, reactive } from 'vue';
const app = createApp();
const router = useRouter()
const route = useRoute()

app.use(Toast);
app.use(Form);
app.use(Field);
app.use(Button);


let rules = reactive({
    username: [
        { required: true, message: '请填写用户名' }
    ],
    password: [
        { required: true, message: '请填写密码' }
    ],
    repass: [
        { required: true, message: '请再次填写密码' }
    ],
    email: [
        { pattern: /^([1-9]\d{4,9}@qq\.com|[a-zA-Z]\w{5,17}@((126|163)\.com|yeah\.net))$/, message: '请输入正确的邮箱' }
    ],
    tel: [
        { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
    ]
})
let username = ref('')
let password = ref('')
let repass = ref('')
let tel = ref('')
let email = ref('')



let onSubmit = (values) => {
    // console.log('submit', values);
    delete values.repass
    register(values).then(data => {
        // console.log(data);
        if (data.error_code === 200) {
            showSuccessToast({
                message: data.msg,
                onClose: () => {
                    router.push('login')
                }
            });
        } else {
            showFailToast({
                message: data.msg
            })
        }
    })
}





</script>

<style></style>
